<?

include ('../session.php');
include ('../db.php');
include ('../functions.php');
include ('../xmlparser.php');
//include ('../newxmlparser.php');
include ('config.php');


/* Prefetch classes and races */
$query = "select race_id, race_name
          from " . DKP_RACES;
$result = mysql_query ($query);

while ($row = mysql_fetch_array ($result))
{
  $raceid = $row['race_id'];
  $racename = strtolower ($row['race_name']);

  $races[$raceid] = $racename;
  $races[$racename] = $raceid;
}

$query = "select class_id, class_name
          from " . DKP_CLASSES;
$result = mysql_query ($query);

while ($row = mysql_fetch_array ($result))
{
  $classid = $row['class_id'];
  $classname = strtolower ($row['class_name']);

  $classes[$classid] = $classname;
  $classes[$classname] = $classid;
}

if ($action == 'add')
{
  /* Convert XML to an array */
  $raid = xml_unserialise ($raidxml);

  /* Fetch a new raidid */
  $query = "select idvalue
            from ids
            where idid = " . ID_RAID;
  $result = mysql_query ($query);

  $row = mysql_fetch_array ($result);

  $raidid = $row['idvalue'];

  /* If this is an old XML */
  if ($raid['RaidInfo'])
  {
    /* Determine the raid date */
    $date = strtotime ($raid['RaidInfo']['start']);
    $date = strftime ("%Y-%m-%d %H:%M:%S", $date);

    /* Determine the raid locationid */
    $location = preg_replace ("/\n/", " ", $raid['RaidInfo']['zone']);

    /* Hacks */
    if ($location == "Ahn'Qiraj Ruins")
    {
      $location = "Ruins of Ahn'Qiraj";
    }

    $c_location = mysql_clean ($location);

    $query = "select location_id
              from " . DKP_LOCATIONS . "
              where location_name = '$c_location'";
    $result = mysql_query ($query);

    $row = mysql_fetch_array ($result);

    $locationid = $row['location_id'];
    $c_locationid = intval ($locationid);

    /* Determine the raid eventid */
    $event = preg_replace ("/\n/", " ", $raid['RaidInfo']['note']);

    if (preg_match ("/(.+) - Zone: .+/", $event, $matches))
    {
      $event = $matches[1];
    }

    /* Hacks */
    if ($event == "Buru")
    {
      $event = "Buru the Gorger";
    }

    $c_event = mysql_clean ($event);

    $query = "select event_id
              from " . DKP_EVENTS . "
              where event_name = '$c_event'
              and location_id = $c_locationid";
    $result = mysql_query ($query);

    $row = mysql_fetch_array ($result);

    $eventid = $row['event_id'];

    /* Create the raid */
    $c_raidid = intval ($raidid);
    $c_eventid = intval ($eventid);
    $c_date = mysql_clean ($date);

    $query = "insert into " . DKP_RAIDS . "
              (raid_id, location_id, event_id, raid_date)
              values ($c_raidid, $c_locationid, $c_eventid, '$c_date')";
    $result = mysql_query ($query);

    /* Parse the members */
    if ($raid['RaidInfo']['PlayerInfos'])
    {
      foreach ($raid['RaidInfo']['PlayerInfos'] as $key => $player)
      {
        $name = $player['name'];
        $level = $player['level'];
        $race = $player['race'];

        /* Hacks */
        if ($race == 'Scourge')
        {
          $race = 'Undead';
        }

        $class = $player['class'];

        $c_name = mysql_clean ($name);

        $query = "select member_id
                  from " . DKP_MEMBERS . "
                  where member_name = '$c_name'";
        $result = mysql_query ($query);

        $row = mysql_fetch_array ($result);

        /* If this is a new member */
        if (!$row)
        {
          /* Fetch a new raidid */
          $query = "select idvalue
                    from ids
                    where idid = " . ID_MEMBER;
          $result = mysql_query ($query);

          $row = mysql_fetch_array ($result);

          $memberid = $row['idvalue'];
          $raceid = $races[strtolower ($race)];
          $classid = $classes[strtolower ($class)];

          $c_memberid = intval ($memberid);
          $c_level = intval ($level);
          $c_raceid = intval ($raceid);
          $c_classid = intval ($classid);

          $query = "insert into " . DKP_MEMBERS . "
                    (member_id, member_name, member_level, race_id, class_id)
                    values ($c_memberid, '$c_name', $c_level, $c_raceid, $c_classid)";
          $result = mysql_query ($query);

          $query = "update ids
                    set idvalue = " . intval ($memberid + 1) . "
                    where idid = " . ID_MEMBER;
          $result = mysql_query ($query);
        }
        else
        {
          $memberid = $row['member_id'];
        }

        /* Add attendance */
        $c_memberid = intval ($memberid);

        $query = "insert into ". DKP_ATTENDEES . "
                  (raid_id, member_id)
                  values ($c_raidid, $c_memberid)";
        $result = mysql_query ($query);
      }
    }
  }
  else
  {
    echo "<pre>" . print_r ($raid, 1) . "</pre>";
  }

  $query = "update ids
            set idvalue = " . intval ($raidid + 1) . "
            where idid = " . ID_RAID;
  $result = mysql_query ($query);
}

$output = '';

$output .= "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
  <head>
    <title>Panic Points - Add Raid</title>
    <link href=\"../general.css\" rel=\"stylesheet\" type=\"text/css\">
  </head>
  <body>";

include ('navigation.php');

$output .= "
    <form name=\"raidform\" action=\"addraid.php\" method=\"POST\">
      <input type=\"hidden\" name=\"action\" id=\"action\" value=\"\">";

$output .= "
      <table>
        <tr valign=\"top\">
          <td>Enter Raid XML</td>
          <td><textarea name=\"raidxml\" id=\"raidxml\" style=\"width: 400px; height: 150px\"></textarea></td>
        </tr>
        <tr>
          <td colspan=\"2\" align=\"right\"><button onclick=\"document.getElementById ('action').value = 'add'; document.getElementById ('raidform').submit ()\">submit</button></td>
        </tr>
      </table>";

$output .= "
    </form>";

$output .= "
  </body>
</html>";

echo $output;

?>
